<?php

class UnreadAnnouncement extends Extension {
	private static $casting = array('TotalUnreadAnnouncement' => 'Int');
	
    function UnreadAnnouncements($limit = 5){
    	$announcements = Announcement::get()->filter('IsActive', 1)->filter('PublishDate:LessThanOrEqual', date('Y-m-d'))->filter('ViewGroups.ID', $this->owner->CurrentMember()->RankID)->limit($limit);
		$exclude = array();
		foreach($announcements as $announcement){
			if($announcement->IsRead()->filter('IsRead.ID', $this->owner->CurrentMember()->ID)->count()){
				$exclude[] = $announcement->ID;
			}
		}
        return $announcements->exclude('ID', $exclude);
    }
	
	function TotalUnreadAnnouncement(){
		$total = 0;
		$announcements = Announcement::get()->filter('IsActive', 1)->filter('PublishDate:LessThanOrEqual', date('Y-m-d'))->filter('ViewGroups.ID', $this->owner->CurrentMember()->RankID);
		foreach($announcements as $announcement){
			if(!$announcement->IsRead()->filter('IsRead.ID', $this->owner->CurrentMember()->ID)->count()){
				$total += 1;
			}
		}
        return $total;
    }
}
